<?php
/**
 * @fileName    miniapp.php
 */
namespace app\manage\controller;
use think\Request;
use app\common\model\member\Member as MemberModel;
use app\common\model\miniapp\MemberMiniapp as AuthorizerModel;
use app\common\model\miniapp\Miniapp as MiniappModel;
use app\common\model\order\Order as OrderModel;
class Miniapp extends Common
{
    //我的小程序
  
    public function index()
    {
        $search = $where = [];
        $AuthorizerModel = new AuthorizerModel();
        $where['member_id'] = $this->member_id;
        $list = $AuthorizerModel->where($where)->order("member_miniapp_id desc")->paginate(10);
        $miniappIds = [];
        foreach ($list as $val) {
            $miniappIds[$val->miniapp_id] = $val->miniapp_id;
        }
        $MiniappModel = new MiniappModel();
        $page = $list->render();
        $miniapp = $MiniappModel->itemsByIds($miniappIds);
        $this->assign('miniapp', $miniapp);
        $this->assign('list', $list);
        $this->assign('page', $page);
        $this->assign('search', $search);
        return $this->fetch();
    }
    

    /**
     * 分配短信条数；
     * @param member_miniapp_id int 小程序id
     * @param sms_num int 短信条数；
     */
     public function smsnum(){
         $member_miniapp_id = (int) $this->request->param('member_miniapp_id');
         $sms_num = (int) $this->request->param('sms_num');
         $AuthorizerModel = new AuthorizerModel();
         if(!$member_minapp = $AuthorizerModel->find($member_miniapp_id)){
             $this->error('不存在该小程序',null,101);
         }
         if($member_minapp->member_id != $this->member_id){
             $this->error('不存在该小程序',null,101);
         }
         $min_sms_num = config('setting.min_sms_num') < 0 ?  1 : config('setting.min_sms_num') ;
         if($sms_num < $min_sms_num ){
             $this->error("最少分配{$min_sms_num}",null,101);
         }
         if($sms_num <= $member_minapp->sms_num){
             $this->error('不得小于剩余的条数',null,101);
         }
         if($sms_num > $this->member_info->sms_num){
             $this->error('您的账户短信剩余数不足请充值',null,101);
         }
         $data['sms_num'] = $sms_num;
         $AuthorizerModel->save($data,['member_miniapp_id'=>$member_miniapp_id]);
         $MemberModel = new MemberModel();
         $member_data['sms_num'] = $this->member_info->sms_num - $sms_num;
         $MemberModel->save($member_data,['member_id'=>$this->member_id]);
         $this->success('短信分配成功',null,101);
     }
     /**
      * 登录后台
      * @param member_miniapp_id int 小程序id
      */
     public function backstage(){
         $member_miniapp_id = (int) $this->request->param('member_miniapp_id');
         $AuthorizerModel = new AuthorizerModel();
         if(!$member_miniapp = $AuthorizerModel->find($member_miniapp_id)){
             $this->error("不存在小程序",null,101);
         }
         if($member_miniapp->member_id != $this->member_id){
             $this->error('不存在小程序',null,101);
         }
         $code = authcode($member_miniapp->member_miniapp_id .'|' . $member_miniapp->appkey . '|miniapp|' . $_SERVER['REQUEST_TIME']);
         cookie('miniapp', $code);
         $this->success('正在进入小程序后台',url('/miniapp/index/index'));
     }

}